package com.android.test.model;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.android.test.util.Constants;
import com.android.test.util.DataUtils;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;

public class BaiduSearchListener implements MKSearchListener {
	
	private final String TAG = getClass().getSimpleName();
	private Context mContext = null;
	private String mKeyWords = null;
	//store the search result info
	public BaiduSearchListener(Context con) {
		mContext = con;
	}
	
	public void setKeyWords(String point){
		mKeyWords = point;
	}
	
	/***********************************************************
	 * 
	 * the result of search address info according to longitude and latitude
	 * 
	 * @param result
	 * 			the address info result
	 * 
	 * @return void
	 * 
	 **********************************************************/
    @Override  
    public void onGetAddrResult(MKAddrInfo result, int iError) { 
    	Log.e(TAG, "onGetAddrResult");
        if (result == null) {
        	Log.e(TAG, "MKAddrInfo result is null");
        	Intent intent = new Intent();
        	intent.setAction(Constants.ACTION_ADDR_RESULT);
			intent.putExtra(Constants.KEY_ADDR_RESULT, "");
			mContext.sendBroadcast(intent);
        	mKeyWords = null;
            return;  
        }  
        StringBuffer sb = new StringBuffer();  
        sb.append(result.strAddr);  
        if (null != result.poiList) {  
            for (MKPoiInfo poiInfo : result.poiList) {
            	if(poiInfo.name.contains(mKeyWords)){
                    sb.append("----------------------------------------\n");  
                    sb.append("名称：").append(poiInfo.name+"\n");  
                    sb.append("地址：").append(poiInfo.address+"\n");
                    sb.append("城市：").append(poiInfo.city+"\n");
                    sb.append("经度：").append(poiInfo.pt.getLongitudeE6() / 1000000.0f+"\n");  
                    sb.append("纬度：").append(poiInfo.pt.getLatitudeE6() / 1000000.0f+"\n");  
                    sb.append("电话：").append(poiInfo.phoneNum+"\n");  
                    sb.append("邮编：").append(poiInfo.postCode+"\n");  
                    sb.append("类型：").append(poiInfo.ePoiType); 
            	}
            } 
            if(sb!=null){
            	//send the result back;
            	Log.e(TAG, "Send result back");
            	Intent intent = new Intent();
            	intent.setAction(Constants.ACTION_ADDR_RESULT);
    			intent.putExtra(Constants.KEY_ADDR_RESULT, sb.toString());
    			mContext.sendBroadcast(intent);
            	mKeyWords = null;
            }
        }  
        
    }  
	/***********************************************************
	 * 
	 * the result of search driving routine info 
	 * 
	 * @param result
	 * 			the driving routine result
	 * 
	 * @return void
	 * 
	 **********************************************************/
	@Override
	public void onGetDrivingRouteResult(MKDrivingRouteResult result, int iError) {
		if(result==null){
			Log.e(TAG,"MKDrivingRouteResult result is null");
			Intent intent = new Intent();
			intent.setAction(Constants.ACTION_DRIVING_RESULT);
			intent.putExtra(Constants.KEY_DRIVING_SEARCH_STATUS, false);
			mContext.sendBroadcast(intent);
			return;
		}else{
			Log.e(TAG, "driving plans：" + result.getNumPlan());  
			DataUtils.mDrivingRouteResult = result;
			Intent intent = new Intent();
			intent.setAction(Constants.ACTION_DRIVING_RESULT);
			intent.putExtra(Constants.KEY_DRIVING_SEARCH_STATUS, true);
			mContext.sendBroadcast(intent);
		}
	}

/*	private void addToPoiResult(ArrayList<OrgDetailInfo> list){
		if(null==DataUtils.mOrgPoiInfo){
			DataUtils.mOrgPoiInfo = new ArrayList<OrgDetailInfo>(0);
			DataUtils.mOrgPoiInfo.addAll(list);
		}
		else{
			for(OrgDetailInfo item:list){
				if(!DataUtils.mOrgPoiInfo.contains(item)){
					DataUtils.mOrgPoiInfo.add(item);
				}
			}
		}
	}*/
	
	/***********************************************************
	 * 
	 * the result of poi info 
	 * 
	 * @param result
	 * 			the poi result
	 * 
	 * @param type
	 * 			(11,12,21) is poi list, 7 is city list
	 * 
	 * @return void
	 * 
	 **********************************************************/
    @Override  
    public void onGetPoiResult(MKPoiResult result, int type, int iError) {  
    	Log.e(TAG, "onGetPoiResult");
    	if(null == result){
    		Log.e(TAG,"MKPoiResult result is null");
    		Intent intent = new Intent();
			intent.setAction(Constants.ACTION_POI_RESULT);
			intent.putExtra(Constants.KEY_POI_SEARCH_STATUS, false);
			mContext.sendBroadcast(intent);
    		return;
    	}
    	if (type == 11 || type == 12 || type == 21){
			//we get some poi info
			ArrayList<MKPoiInfo> poiList = result.getAllPoi();
			if (poiList == null){
				return;
			}
			int poiCount = poiList.size();
			if(null==DataUtils.mOrgPoiInfo){
				DataUtils.mOrgPoiInfo = new ArrayList<OrgDetailInfo>(0);
			}
			for (int i = 0; i < poiCount; i++){
				//store the poi info to array
				final OrgDetailInfo info = new OrgDetailInfo();
				info.shortName = poiList.get(i).name;
				info.mainAddress = poiList.get(i).address;
				info.mGpoint = poiList.get(i).pt;
				info.telephoneNum = poiList.get(i).phoneNum;
				info.mPostCode = poiList.get(i).postCode;
				info.mCity = poiList.get(i).city;
				Log.e(TAG, poiList.get(i).name);
				if(!DataUtils.mOrgPoiInfo.contains(info)){
					DataUtils.mOrgPoiInfo.add(info);
				}
			}
			Log.e(TAG, "Send poi result");
			Intent intent = new Intent();
			intent.setAction(Constants.ACTION_POI_RESULT);
			intent.putExtra(Constants.KEY_POI_SEARCH_STATUS, true);
			mContext.sendBroadcast(intent);
		}else{
			Log.e(TAG,"type =" +type);
			Intent intent = new Intent();
			intent.setAction(Constants.ACTION_POI_RESULT);
			intent.putExtra(Constants.KEY_POI_SEARCH_STATUS, false);
			mContext.sendBroadcast(intent);
		}
    }  
	/***********************************************************
	 * 
	 * the result of bus routine
	 * 
	 * @param result
	 * 			the result
	 * 
	 * @return void
	 * 
	 **********************************************************/
	@Override
	public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {
		if(result==null){
			Log.e(TAG,"MKTransitRouteResult result is null");
			Intent intent = new Intent();
			intent.setAction(Constants.ACTION_TRANSIT_RESULT);
			intent.putExtra(Constants.KEY_TRANSIT_SEARCH_STATUS, false);
			mContext.sendBroadcast(intent);
			return;
		}else{
			Log.e(TAG, "bus transmit plans：" + result.getNumPlan());  
			DataUtils.mTransitRouteResult = result;
			Intent intent = new Intent();
			intent.setAction(Constants.ACTION_TRANSIT_RESULT);
			intent.putExtra(Constants.KEY_TRANSIT_SEARCH_STATUS, true);
			mContext.sendBroadcast(intent);
		}
	}

	
	/***********************************************************
	 * 
	 * the result of walk routine
	 * 
	 * @param result
	 * 			the result
	 * 
	 * @return void
	 * 
	 **********************************************************/
    @Override  
    public void onGetWalkingRouteResult(MKWalkingRouteResult result, int iError) {  
    	if(result==null){
			Log.e(TAG,"MKWalkingRouteResult result is null");
			Intent intent = new Intent();
			intent.setAction(Constants.ACTION_WALKING_RESULT);
			intent.putExtra(Constants.KEY_WALKING_SEARCH_STATUS, false);
			mContext.sendBroadcast(intent);
			return;
		}else{
			Log.e(TAG, "walking plans：" + result.getNumPlan());  
			DataUtils.mWalkingRouteResult = result;
			Intent intent = new Intent();
			intent.setAction(Constants.ACTION_WALKING_RESULT);
			intent.putExtra(Constants.KEY_WALKING_SEARCH_STATUS, true);
			mContext.sendBroadcast(intent);
		}
    } 

}
